<template>
  <el-form 
    ref="formRef"
    :model="formData" 
    :rules="formRules"
    label-position="top"
    @submit.prevent="handleSubmit"
  >
    <el-form-item label="文章标题" prop="title">
      <el-input v-model="formData.title" placeholder="请输入文章标题" />
    </el-form-item>
    
    <el-form-item label="分类" prop="category">
      <el-select 
        v-model="formData.category" 
        placeholder="请选择分类"
        style="width: 100%"
      >
        <el-option 
          v-for="item in categories" 
          :key="item.value" 
          :label="item.label" 
          :value="item.value" 
        />
      </el-select>
    </el-form-item>
    
    <el-form-item label="摘要" prop="summary">
      <el-input 
        v-model="formData.summary" 
        type="textarea" 
        :rows="3"
        placeholder="请输入文章摘要" 
      />
    </el-form-item>
    
    <el-form-item label="文章内容" prop="content">
      <el-input 
        v-model="formData.content" 
        type="textarea" 
        :rows="10"
        placeholder="请输入文章内容" 
      />
    </el-form-item>
    
    <el-form-item>
      <el-button 
        type="primary" 
        @click="handleSubmit"
        :loading="loading"
      >
        保存文章
      </el-button>
      <el-button @click="$router.push('/dashboard')">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script setup>
import { ref, onMounted, watch } from 'vue'
import { useRouter } from 'vue-router'
import axios from 'axios'

const props = defineProps({
  post: {
    type: Object,
    required: true
  },
  loading: {
    type: Boolean,
    default: false
  }
})

const emit = defineEmits(['submit'])
const router = useRouter()
const formRef = ref(null)

const formData = ref({
  title: '',
  category: '',
  summary: '',
  content: ''
})

const categories = ref([
  { value: 'technology', label: '技术' },
  { value: 'life', label: '生活' },
  { value: 'design', label: '设计' },
  { value: 'other', label: '其他' }
])

const formRules = {
  title: [
    { required: true, message: '请输入文章标题', trigger: 'blur' },
    { min: 3, max: 100, message: '标题长度应在3到100个字符之间', trigger: 'blur' }
  ],
  category: [
    { required: true, message: '请选择文章分类', trigger: 'change' }
  ],
  summary: [
    { required: true, message: '请输入文章摘要', trigger: 'blur' },
    { min: 10, max: 200, message: '摘要长度应在10到200个字符之间', trigger: 'blur' }
  ],
  content: [
    { required: true, message: '请输入文章内容', trigger: 'blur' },
    { min: 20, message: '内容至少需要20个字符', trigger: 'blur' }
  ]
}

const handleSubmit = async () => {
  if (!formRef.value) return
  
  await formRef.value.validate((valid) => {
    if (valid) {
      emit('submit', {
        title: formData.value.title,
        category: formData.value.category,
        summary: formData.value.summary,
        content: formData.value.content
      })
    }
  })
}

// Watch for changes in the post prop to update form data
watch(() => props.post, (newPost) => {
  if (newPost) {
    formData.value = {
      title: newPost.title || '',
      category: newPost.category || '',
      summary: newPost.summary || '',
      content: newPost.content || ''
    }
  }
}, { immediate: true })
</script>

